package game;

import heroes.Hero;

import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import cards.Card;
import cards.RandomCard;

public class CardChoosingDialog extends JDialog implements CardContainer {
	View myView;
	boolean answerable = false;
	private String targetName;

	private JPanel actualCardPanel;
	private JScrollPane cardScrollPane;

	/**
	 * @return the targetName
	 */
	public String getTargetName() {
		return targetName;
	}

	/**
	 * @param targetName
	 *            the targetName to set
	 */
	public void setTargetName(String targetName) {
		this.targetName = targetName;
	}

	public static int EDGE;
	public static int CardChoosingDialog_WIDTH;
	public static int CardChoosingDialog_HEIGHT;

	public static void initPreferredSize() {
		EDGE = 5;
		// considering there will be two rows and 4 columns in the
		// CardChoosingDialog
		CardChoosingDialog_WIDTH = Card.CARD_WIDTH * 4 + EDGE * 5;
		CardChoosingDialog_HEIGHT = Card.CARD_HEIGHT + 25;//+ EDGE * 3;
		System.out.println("CardChoosingDialog_WIDTH "
				+ CardChoosingDialog_WIDTH);
		System.out.println("CardChoosingDialog_HEIGHT "
				+ CardChoosingDialog_HEIGHT);
	}

	public CardChoosingDialog(final View myView) {
		super(myView, "Choose the card");
		setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		this.myView = myView;
		setSize(new Dimension(CardChoosingDialog_WIDTH,
				CardChoosingDialog_HEIGHT));
//		setLayout(new FlowLayout());
		// setPreferredSize(new Dimension(CardChoosingDialog_WIDTH,
		// CardChoosingDialog_HEIGHT));
		actualCardPanel = new JPanel();
		actualCardPanel.setBackground(Color.LIGHT_GRAY);
		actualCardPanel.setLayout(new GridLayout(1, 0, 0, 0));

		cardScrollPane = new JScrollPane(actualCardPanel,
				JScrollPane.VERTICAL_SCROLLBAR_NEVER,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		add(cardScrollPane);
		// this.setVisible(true);
		this.setAlwaysOnTop(true);
		//@deprecated, click detection is done by Card
//		addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseClicked(MouseEvent e) {
//				if (answerable) {
//					for (Card every : cards) {
//						if (((Card) e.getSource()).equalsCard(every)) {
//							answerable = false;
//							try {
//								clickUpdate(every);
//							} catch (InterruptedException e1) {
//								// TODO Auto-generated catch block
//								e1.printStackTrace();
//							}
//							break;
//						}
//					}
//				}
//			}
//		});
		// this.pack();
	}

	ArrayList<Card> cards = new ArrayList<Card>();

	public void addInvisibleCards(int n) {
		System.out.println("addInvisibleCards() n=" + n);
		for (int i = n; i > 0; i--) {
			Card newInvisiCard = new RandomCard();
			addCard(newInvisiCard);
		}
	}

	public void addCard(Card c) {
		System.out.println("CardChoosingDialog.addCard() " + c.toString());
		cards.add(c);
		c.linkCardContainer(this);
		actualCardPanel.add(c);
		c.setClickable(true);
//		revalidate();
		invalidate();
		validate();
		repaint();
	}

	public void removeCard(Card toBremoved) {
		toBremoved.delink();
		cards.remove(toBremoved);
		actualCardPanel.remove(toBremoved);
	}

	public void clickUpdate(Card newUpdate) throws InterruptedException {
		myView.cardChosen(targetName, newUpdate);
		// this.disable();
		setEnabled(false);
		dispose();
	}
}
